package com.hq.springsessiondemo.controller;

import com.hq.springsessiondemo.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @GetMapping("/login")
    public String login(String name, HttpServletRequest request) {
        if ("admin".equals(name)) {
            request.getSession().setAttribute("login-user", new User(name,""));
            return "欢迎【"+name+"】:"+System.currentTimeMillis();
        }
        return "用户或密码错误";
    }

    @GetMapping("/findUser")
    public String findUser(HttpServletRequest request, HttpSession session) {
        HttpSession requestSession = request.getSession();
        Object attribute = requestSession.getAttribute("login-user");
        System.out.println(requestSession.getClass());
        System.out.println("session"+session.getClass());
        System.out.println(session.getAttribute("login-user"));
        return "【" + attribute + "】";
    }
}